package com.example.deliveryapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.deliveryapplication.entity.BearingModel
import com.example.deliveryapplication.entity.BearingType
import com.example.deliveryapplication.entity.Client
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.sql.AppDatabase
import com.example.deliveryapplication.wiget.AddBearModelDialog
import com.example.deliveryapplication.wiget.AddClientDialog
import kotlinx.android.synthetic.main.activity_bearing_model_set.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class BearingModelSetActivity : AppCompatActivity() ,CenterStringListAdapter.CallBack{
    private val bearDao by lazy { AppDatabase.getDatabase(this).bearingModelDao() }
    private val bearTypeDao by lazy { AppDatabase.getDatabase(this).bearingTypeDao() }
    private val planItemDao by lazy { AppDatabase.getDatabase(this).planItemDao() }
    private val mList by lazy {ArrayList<SelectString>()  }
    private val typeList by lazy { ArrayList<SelectString>() }
    private val mAdapter by lazy { CenterStringListAdapter(this,mList,this) }
    private val mViewModel by lazy { ViewModelProvider(this)[BearingModelViewModel::class.java] }
    private val tViewModel by lazy { ViewModelProvider(this) [BearingTypeViewModel::class.java]}
    private var selectPosition=-1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bearing_model_set)
        mRecycleView.layoutManager= LinearLayoutManager(this)
        mRecycleView.adapter=mAdapter
        mConfirmBTN.setOnClickListener {
            val dialog=AddBearModelDialog(this)
            dialog.setTypeList(typeList)
            dialog.setListener(object :AddBearModelDialog.CallBack{
                override fun onConfirmClick(name: String, data: SelectString) {
                    lifecycleScope.launch(Dispatchers.IO) {
                        val uuid = getNewUUid()
                        var client= BearingModel(uuid,name,data.code,data.name)
                        bearDao.insertChart(client)
                        initData()
                    }
                }

            })
            dialog.show()
        }
        initData()
        mViewModel.updateResult.observe(this){
            initData()
        }
        mViewModel.result.observe(this){list->
            list.forEach {
                val selectString= SelectString()
                selectString.param["typeId"]=it.typeId
                selectString.param["name"]=it.name
                selectString.code=it.id
                selectString.name="${it.type}-${it.name.handlerNull()}"
                mList.add(selectString)
            }
            mAdapter.notifyDataSetChanged()
        }
        tViewModel.result.observe(this){list->
            typeList.clear()
            list.forEach {
                val selectString=SelectString()
                selectString.name=it.name
                selectString.code=it.id
                typeList.add(selectString)
            }
        }
        mViewModel.deleteResult.observe(this){
            when(it){
                -1->{
                    showToast("该型号已经在某个计划表中被使用，请先删除计划表中的数据")
                }
                0->{
                    showToast("删除失败")
                }
                else->{
                    showToast("删除成功")
                    mAdapter.removeAt(selectPosition,mList.size)
                    mList.removeAt(selectPosition)
                }
            }
        }
    }


    override fun onItemClick(position: Int, data: SelectString) {

    }

    override fun onItemDelete(position: Int, data: SelectString) {
        selectPosition=position
        mViewModel.deleteModel(planItemDao,bearDao,data.code)
    }

    override fun onItemEdit(position: Int, data: SelectString) {
        val dialog=AddBearModelDialog(this)
        dialog.setEdit(true)
        dialog.setTypeList(typeList)
        dialog.setData(data)
        dialog.setListener(object :AddBearModelDialog.CallBack{
            override fun onConfirmClick(name: String, type: SelectString) {
               var bear= BearingModel(data.code,name,type.code,type.name)
                mViewModel.updateModel(bearDao,bear)
            }
        })
        dialog.show()
    }

    private fun getData(){
        mViewModel.getAll(bearDao)
    }
    private fun initData(){
        mList.clear()
        getData()
        tViewModel.getAll(bearTypeDao)
    }
}